package cn.tx.security.service.impl;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.List;

/**
 * @Package: cn.tx.security.service
 * @Description: 类作用描述
 * @Author Mxb
 * @CreateDate 2020/10/31 16:51
 * @Version 1.0
 */
//@Service
public class LoginService implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 判断用户名是否存在
        if (!"mary".equals(username)) {
            throw new UsernameNotFoundException("用户名不存在！");
        }
        // 从数据库中获取的密码 123456 的密文
        String pwd = "$2a$10$NApFIvMZGR3.pNSolS3Uf./FhIprUfXGQNk0rJRbGjMVPakea0Uai";
        // 第三个参数表示权限
        List<GrantedAuthority> auths =
                AuthorityUtils
                        .commaSeparatedStringToAuthorityList("admin,role,ROLE_admin,ROLE_role");
        return new User(username, pwd, auths);
    }
}
